package com.example.service.impl;

import com.example.mapper.EmpExprMapper;
import com.example.mapper.EmpLogMapper;
import com.example.mapper.EmpMapper;
import com.example.pojo.*;
import com.example.service.EmpService;
import com.example.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class EmpServiceimpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogMapper empLogMapper;

    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        //2.执行查询
        List<Emp> emplist = empMapper.list(empQueryParam);
        //3.封装查询结果
        Page<Emp> p = (Page<Emp>) emplist;
        return new PageResult<Emp>(p.getTotal(), p.getResult());

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {

        try {
            //1.保存员工信息
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);
            //2.保存员工工作经历
            List<EmpExpr> exprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(exprList)) {
                //遍历集合,赋值id来绑定上面的员工信息
                exprList.forEach(empExpr -> {
                    empExpr.setEmpId(emp.getId());
                });
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            //记录操作日志
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "新增员工:" + emp);
            empLogMapper.insert(empLog);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> ids) {
        //根据id删除员工基本信息
        empMapper.deleteEmp(ids);
        //根据id删除员工工作信息
        empMapper.deleteExpr(ids);
    }


    @Override
    public Emp selectByID(Integer id) {
        Emp emp = empMapper.selectByID(id);
        return emp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        //根据id更新员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);

        //根据id删除员工工作信息
        //先根据员工id删除原先的工作经历
        empMapper.deleteExpr(Arrays.asList(emp.getId()));
        //再添加员工新的工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            //这里因为是分开赋值，相当于模块复用，而在旧模块这里是两个表是分开的，所以为了让他们连在一起保证数据一致就再次赋值
            //前端发送请求时没有使用id这个属性，需要在这里重新获取
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }

    }

    //登录
    public LoginInfo login(Emp emp) {
        //1.调用mapper接口进行查询
        Emp e = empMapper.login(emp);

        //2.判断并组装登录信息
        if (e != null) {
            log.info("登录信息:{}", e);
            //生成jwt令牌
            HashMap<String, Object> claims = new HashMap<>();
            //存储唯一标识id来分辨登录用户
            claims.put("id", e.getId());
            claims.put("username", e.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            //返回数据
            return new LoginInfo(e.getId(), e.getUsername(), e.getName(), jwt);
        }
        //3.不存在则返回null
        return null;
    }
}




